console.log(window.location.hash.substring(1));
// 数据回显
function getStudent(id) {
    let pro = getAjax("http://localhost:3008/api/student/getStudent?id=" + id);
    pro.then(data => {
        let stuobj = JSON.parse(data);
        console.log(stuobj);
        document.getElementById("class").value = stuobj[0].clazz;
        document.getElementById("name").value = stuobj[0].name;
        document.getElementById("age").value = stuobj[0].age;
        document.getElementById("tel").value = stuobj[0].tel;
        document.getElementById("beizhu").value = stuobj[0].remark;
        // 性别回显
        let sex = document.getElementsByName("sex");
        // if (stuobj[0].gender == "男") {
        //     sex[0].checked = true;
        // } else {
        //     sex[1].checked = true;
        // }
        document.querySelector("input[value=" + stuobj[0].gender + "]").checked = true;
        // 爱好回显


        let hobby = stuobj[0].hobby;
        let aihao = document.getElementsByName("aihao");
        if (typeof (hobby) == "object") {
            if (hobby.includes("吃饭")) {
                aihao[0].checked = true;
            }
            if (hobby.includes("睡觉")) {
                aihao[1].checked = true;
            }
            if (hobby.includes("学习")) {
                aihao[2].checked = true;
            }
        } else if (typeof (hobby) == "string") {
            let hobbyobj = hobby.split(",");
            if (hobbyobj.includes("吃饭")) {
                aihao[0].checked = true;
            }
            if (hobbyobj.includes("睡觉")) {
                aihao[1].checked = true;
            }
            if (hobbyobj.includes("学习")) {
                aihao[2].checked = true;
            }
        }


        // address回显
        let address = stuobj[0].address;
        console.log(address);
        let selectlist = document.getElementById("address");
        let optionlist = selectlist.getElementsByTagName("option");
        if (address == "郑州") {
            optionlist[0].selected = true;
        } else if (address == "洛阳") {
            optionlist[1].selected = true;
        } else {
            optionlist[2].selected = true;
        }


    })
}
let stuid = window.location.hash.substring(1);
getStudent(stuid);

// 修改
function modifyStudent() {
    // 班级
    let clazz = document.getElementById("class").value;
    // 名字
    let name = document.getElementById("name").value;
    // 性别
    let radio = document.getElementsByName("sex")[0].checked;
    var radiovalue;
    if (radio == true) {
        radiovalue = "男"
    } else {
        radiovalue = '女'
    }
    // 年龄
    let age = document.getElementById("age").value;
    // 爱好
    let checkboxarr = document.getElementsByName("aihao");
    let selectchecked = [];

    for (let i = 0; i < checkboxarr.length; i++) {
        if (checkboxarr[i].checked == true) {
            selectchecked.push(checkboxarr[i].value);
        }
    }
    // 手机号
    let tel = document.getElementById("tel").value;
    // 地址
    let selectlist = document.getElementById("address");
    let optionlist = selectlist.getElementsByTagName("option");
    let address;
    for (let i = 0; i < optionlist.length; i++) {
        if (optionlist[i].selected == true) {
            address = optionlist[i].value;
        }
    }

    // 备注
    let beizhu = document.getElementById("beizhu").value;
    // 时间
    let data = document.getElementById("date").value;
    console.log(data);


    let add = postAjax("http://localhost:3008/api/student/updateStudent", `id=${stuid}&clazz=${clazz}&name=${name}&gender=${radiovalue}&age=${age}&hobby=${selectchecked}&tel=${tel}&address=${address}&remark=${beizhu}&date=${data}`);
    add.then((data) => {
        alert("修改成功");
        location.href = './home.html'
    }).catch(data => {
        alert("修改成功");

    })
}